<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\helpers\UploadService;
use app\traits\JsonResponse;
use think\exception\ValidateException;
use think\Request;

class Attachment extends BaseController
{
    use JsonResponse;
    protected $middleware = [
        'auth_jwt' => ['only' => ['image']],
    ];
    public function image(){
        // 获取表单上传文件
        $files = request()->file();
        try {
            validate(['image'=>'fileSize:10485760|fileExt:jpg,png,jpeg,gif'])->check($files);
            $file = request()->file('image');
            $savename = app(UploadService::class)->image($file,'topic');
            return  $this->jsonSuccess(['path'=>$savename]);

        } catch (ValidateException $e) {
            return $this->jsonFail($e->getError());
        }
    }

}
